package com.qiwx.pigeon.fragment.query;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DateTimeUtils;
import com.qiwx.pigeon.activity.MainActivity;
import com.qiwx.pigeon.adapter.query.QueryPigeonFactoryAdapter;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseMVPFragment;
import com.qiwx.pigeon.data.params.QueryWareHouseParam;
import com.qiwx.pigeon.data.vo.QueryColumnNameBean;
import com.qiwx.pigeon.data.vo.QueryWareHouseBean;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.presenter.QueryWHPresenter;
import com.qiwx.pigeon.viewinterface.IQueryWHView;
import com.qiwx.pigeon.widget.itemderection.CustomGridItemDecoration;

import org.angmarch.views.NiceSpinner;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.qiwx.pigeon.activity.common.SelectDateActivity.REQUEST_CODE_END;
import static com.qiwx.pigeon.activity.common.SelectDateActivity.REQUEST_CODE_START;

/**
 * @description
 * @Author qiwx
 * @Date 2020-03-28 10:52
 * 鸽厂情况查询
 **/
public class QueryPigeoFactoryFragment extends BaseMVPFragment<IQueryWHView, QueryWHPresenter> implements IQueryWHView {
    @BindView(R.id.spinner_wh)
    NiceSpinner spinnerWh;
    @BindView(R.id.txt_start_date)
    TextView txtStartDate;
    @BindView(R.id.txt_end_date)
    TextView txtEndDate;
    @BindView(R.id.btn_condition)
    Button btnCondition;
    @BindView(R.id.btn_query)
    Button btnQuery;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.txt_nodata)
    TextView txtNodata;
    @BindView(R.id.lin_nodata)
    LinearLayout linNodata;
    List<QueryWareHouseBean> list = new ArrayList<>();
    QueryPigeonFactoryAdapter adapter;
    int type;//0 鸽仓查询 1是鸽场查询
    //int currentWh_id;//选择后鸽仓id
    //int wh_number;//选择后鸽仓number
    String startDate = "";
    String endDate = "";
    QueryWareHouseParam queryWareHouseParam = new QueryWareHouseParam();
    //当前查询wh_id，每次onresume检查 查询鸽仓id是否与当前一致，不一致则刷新数据
    int query_wh_id;//

    @Override
    public int getLayoutId() {
        return R.layout.fragment_query_pigeo_factory;
    }

    @Override
    public void initAfterOnCreate() {
        super.initAfterOnCreate();
        queryWareHouseParam.setPageSize(30);
        queryWareHouseParam.setPageNum(1);
        queryWareHouseParam.setF_id(AppContext.getFarmId(getContext()));
        initView();
        //loadData(type);
    }

    @Override
    public QueryWHPresenter createPresenter() {
        return new QueryWHPresenter(new WeakReference(this));
    }

    @Override
    public void onResume() {
        super.onResume();
        changeData();

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle bundle = this.getArguments();
        if (bundle != null) {
            type = bundle.getInt("type");
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private void initView() {
//        if (type == 1) {
//            spinnerWh.setVisibility(View.GONE);
//        } else {
//            spinnerWh.setVisibility(View.VISIBLE);
//            initSpinner();
//        }
        spinnerWh.setVisibility(View.GONE);
        btnCondition.setVisibility(View.GONE);
        btnQuery.setText("刷新");
        //默认查询当前日期30天之前
        long start = DateTimeUtils.addDay(new Date(), -30).getTime();
        long end = DateTimeUtils.getCurrentStamp();
        txtStartDate.setText(DateTimeUtils.timeStamp2Date(start));
        txtEndDate.setText(DateTimeUtils.timeStamp2Date(end));
        adapter = new QueryPigeonFactoryAdapter(type, R.layout.adapter_query_pigeon_factory_list_item, list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.addItemDecoration(new CustomGridItemDecoration(2));
        recyclerview.setAdapter(adapter);
    }

    //刷新Date数据
    private void onReFreshDate() {
        startDate = txtStartDate.getText().toString();
        endDate = txtEndDate.getText().toString();
    }

    //    private void initSpinner() {
//
//        List<WareHouseBean> list = AppContext.houses;
//        List<String> data = new ArrayList<>();
//        for (int i = 0; i < list.size(); i++) {
//            data.add(list.get(i).getNumber() + "号仓");
//        }
//        spinnerWh.attachDataSource(data);
//        spinnerWh.setOnSpinnerItemSelectedListener((parent, view, position, id) -> {
//            currentWh_id = list.get(position).getId();
//            wh_number = list.get(position).getNumber();
//            loadData(type);
//
//        });
//        if (list.size() > 0) {
//            currentWh_id = list.get(0).getId();
//            wh_number = list.get(0).getNumber();
//        }
//    }
    public void loadData() {
        loadData(type);
    }

    public void changeData() {
        if (query_wh_id != MainActivity.wh_id) {
            loadData(type);
        }
    }

    public void loadData(int type) {
        onReFreshDate();
        list.clear();
        query_wh_id = MainActivity.wh_id;
        queryWareHouseParam.setWh_id(query_wh_id);

        queryWareHouseParam.setEndDate(endDate);
        queryWareHouseParam.setStartDate(startDate);
        queryWareHouseParam.setType(type);
        mPresenter.queryWareHouseByPage(queryWareHouseParam);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_START:
                if (data != null && !TextUtils.isEmpty(data.getStringExtra("car_Time_MSG"))) {
                    txtStartDate.setText(data.getStringExtra("car_Time_MSG"));
                    startDate = txtStartDate.getText().toString();
                }
                break;
            case REQUEST_CODE_END:
                if (data != null && !TextUtils.isEmpty(data.getStringExtra("car_Time_MSG"))) {
                    txtEndDate.setText(data.getStringExtra("car_Time_MSG"));
                    endDate = txtEndDate.getText().toString();
                }
                break;
        }
    }

    @OnClick({R.id.txt_start_date, R.id.txt_end_date, R.id.btn_condition, R.id.btn_query})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_start_date:
                selectStartValiDate();
                break;
            case R.id.txt_end_date:
                selectEndValiDate(txtStartDate.getText().toString());
                break;
            case R.id.btn_condition:
                break;
            case R.id.btn_query:
                loadData(type);
                break;
        }
    }

    @Override
    public void onQueryWareHouseSuccess(PageInfo<QueryWareHouseBean> data) {
        //手动添加第一列
        QueryWareHouseBean bean = new QueryWareHouseBean();
        QueryColumnNameBean column = new QueryColumnNameBean();
        column.setLayEggAmount("产蛋量(窝)");
        column.setBadEgg("坏蛋量(窝)");
        column.setEggAmount1("1个蛋(窝)");
        column.setEggAmount2("2个蛋(窝)");
        column.setShoujing1("1受精(窝)");
        column.setSijing1("1死精(窝)");
        column.setWujing1("1无精(窝)");
        column.setShoujing2("2受精(窝)");
        column.setSijing2("2死精(窝)");
        column.setWujing2("2无精(窝)");
        column.setShoujing2sijing1("1受1死(窝)");
        column.setShoujing2wujing1("1受1无(窝)");
        column.setWujing2sijing1("1无1死(窝)");
        column.setNotfudan("不孵蛋(窝)");
        column.setBreakenEgg("踩破蛋(窝)");
        column.setWudan("窝里无蛋(窝)");
        column.setCunlan("种鸽存栏(对)");
        column.setCkAmount("出壳数量(只)");
        column.setDeathAmount("鸽子死亡数(只)");
        column.setChulan("出栏数量(只)");
        bean.setColumn(column);
        list.add(0, bean);
        list.addAll(data.getList());
        if (type == 1) {
            //手动添加总和
            QueryWareHouseBean sumBean = new QueryWareHouseBean();
            sumBean.setWh_number(0);
            for (int i = 1; i < list.size(); i++) {
                QueryWareHouseBean b = list.get(i);
                sumBean.setBean(b);
            }
            list.add(sumBean);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(String error) {

    }
}
